package com.llh.cheap.repositories;

import top.moirae.core.AbstractOption;
import org.intellij.lang.annotations.Language;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import top.moirae.jdbc.MysqlJDBCSupport;
import top.moirae.repository.PaginationData;
import top.moirae.repository.PaginationForm;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import com.llh.cheap.models.CheapUser;
import com.llh.cheap.mappers.CheapUserMapper;
import com.llh.cheap.forms.CheapUserForm;

import java.util.List;

/**
 * @author lilinhan
 **/
@Named
public class CheapUserRepository extends MysqlJDBCSupport {

    @Inject
    protected CheapUserRepository(@Named("BUSINESS_DB") JdbcTemplate db, @Named("BUSINESS_NAMED_DB") NamedParameterJdbcTemplate namedDB) {
        super(db, namedDB);
    }

    @Language("SQL")
    final String column = "select cu.`id`,cu.`user_id`,cu.`boss_id`,cu.`wechat_name`,cu.`customer_number`,cu.`qxt_code`,cu.`qxt_share`,cu.`qxt_account`,cu.`qxt_password`,cu.`employee_number`,cu.`recommend_name`,u1.id u1_id,u1.name u1_name,u1.mobile u1_mobile,u1.portrait u1_portrait from `cheap_user` cu left join `user` u1 on u1.id=cu.user_id where 1=1 ";

    public AbstractOption<CheapUser> option(Long id) {
        @Language("SQL") String sql = column + " and cu.id = :id ";
        MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue("id", id);
        return option(sql, CheapUserMapper.DETAIL, params);
    }

    public int delete(Long id) {
        @Language("SQL") String sql = "delete from `cheap_user` where id = :id  ";
        MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue("id", id);
        return update(sql, params);
    }


    public long create(CheapUserForm.Create form) {
        return super.autoInsert(form);
    }

    public long update(long id, CheapUserForm.Update form) {
        return super.autoUpdate(id, form);
    }

    public boolean isExist(long id) {
        @Language("SQL") String sql = "select COUNT(0) from `cheap_user` where id = :id";
        MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue("id", id);
        return object(sql, Long.class, params) > 0;
    }

    public List<CheapUser> list(CheapUserForm.Search form) {
        MapSqlParameterSource params = new MapSqlParameterSource();
        String condition = SEARCH_FORM_CONDITION.apply(params, form);
        return list(column + condition, CheapUserMapper.DETAIL, params);
    }

    public PaginationData<CheapUser> pageList(CheapUserForm.Search form, PaginationForm paginationForm) {
        MapSqlParameterSource params = new MapSqlParameterSource();
        String condition = SEARCH_FORM_CONDITION.apply(params, form);
        return paged(column + condition, CheapUserMapper.DETAIL, params, paginationForm);
    }
}

